Page({
  data: {
    note: {
      id: '',
      title: '',
      content: '',
    },
  },
  onLoad: function(options) {
    if (options.id) {
      const notes = wx.getStorageSync('notes') || [];
      const note = notes.find(n => n.id === options.id);
      this.setData({ note });
    }
  },
  editTitle: function(e) {
    this.setData({
      'note.title': e.detail.value
    });
  },
  editContent: function(e) {
    this.setData({
      'note.content': e.detail.value
    });
  },
  saveNote: function() {
    const note = this.data.note;
    if (!note.title.trim()) return;

    let notes = wx.getStorageSync('notes') || [];
    const index = notes.findIndex(n => n.id === note.id);
    if (index !== -1) {
      notes[index] = note;
    } else {
      notes.push({
        id: Date.now() + '',
        title: note.title,
        content: note.content
      });
    }
    wx.setStorageSync('notes', notes);
    wx.navigateBack();
  },
});